package com.wildlife.springboot.mapper.service;

import com.wildlife.springboot.entity.EntityRelationship;
import com.wildlife.springboot.repository.EntityRepository;
import org.neo4j.driver.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.neo4j.driver.*;

import java.util.ArrayList;
import java.util.List;

import static org.neo4j.driver.Values.parameters;

@Service
public class GraphService {
    @Autowired
    private EntityRepository entityRepository;

    private final Driver driver;

    public GraphService(Driver driver) {
        this.driver = driver;
    }

    // 获取所有实体关系
    public List<EntityRelationship> getEntityRelationships() {
        try (Session session = driver.session()) {
            String cypherQuery = "MATCH (a:Entity)-[r]->(b:Entity) RETURN a.name AS entity1, type(r) AS relationship, b.name AS entity2";
            Result result = session.run(cypherQuery);
            List<EntityRelationship> relationships = new ArrayList<>();
            while (result.hasNext()) {
                Record record = result.next();
                EntityRelationship entityRelationship = new EntityRelationship();
                entityRelationship.setEntity1(record.get("entity1").asString());
                entityRelationship.setRelationship(record.get("relationship").asString());
                entityRelationship.setEntity2(record.get("entity2").asString());
                relationships.add(entityRelationship);
            }
            return relationships;
        }
    }

    // 增加实体关系
    public String addEntityRelationship(EntityRelationship entityRelationship) {
        try (Session session = driver.session()) {
            String cypherQuery = "MERGE (a:Entity {name: $entity1}) " +
                    "MERGE (b:Entity {name: $entity2}) " +
                    "MERGE (a)-[r:" + entityRelationship.getRelationship() + "]->(b)";
            session.run(cypherQuery, parameters("entity1", entityRelationship.getEntity1(), "entity2", entityRelationship.getEntity2()));
            return "Entity relationship added successfully!";
        }
    }

    // 删除实体关系
    public String deleteEntityRelationship(EntityRelationship entityRelationship) {
        try (Session session = driver.session()) {
            String cypherQuery = "MATCH (a:Entity)-[r:" + entityRelationship.getRelationship() + "]->(b:Entity) " +
                    "WHERE a.name = $entity1 AND b.name = $entity2 " +
                    "DELETE r";
            session.run(cypherQuery, parameters("entity1", entityRelationship.getEntity1(), "entity2", entityRelationship.getEntity2()));
            return "Entity relationship deleted successfully!";
        }
    }

    // 根据实体查询所有相关关系
    public List<EntityRelationship> getEntityRelationshipsByEntity(String entity) {
        try (Session session = driver.session()) {
            String cypherQuery = "MATCH (a:Entity)-[r]->(b:Entity) " +
                    "WHERE a.name = $entity OR b.name = $entity " +
                    "RETURN a.name AS entity1, type(r) AS relationship, b.name AS entity2";
            Result result = session.run(cypherQuery, parameters("entity", entity));
            List<EntityRelationship> relationships = new ArrayList<>();
            while (result.hasNext()) {
                Record record = result.next();
                EntityRelationship entityRelationship = new EntityRelationship();
                entityRelationship.setEntity1(record.get("entity1").asString());
                entityRelationship.setRelationship(record.get("relationship").asString());
                entityRelationship.setEntity2(record.get("entity2").asString());
                relationships.add(entityRelationship);
            }
            return relationships;
        }
    }
}

